package com.cyq.dascma.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.cyq.dascma.constant.ValidaEnum;
import com.cyq.dascma.dto.ClazzInfoDto;
import com.cyq.dascma.dto.RuleDto;
import com.cyq.dascma.entity.Clazz;
import com.cyq.dascma.entity.Rule;
import com.cyq.dascma.exception.ServiceException;
import com.cyq.dascma.handler.valida.impl.ClazzValidaImpl;
import com.cyq.dascma.handler.valida.impl.RuleValidaImpl;
import com.cyq.dascma.result.Result;
import com.cyq.dascma.result.ResultGenerator;
import com.cyq.dascma.service.RuleService;
import com.cyq.dascma.util.RequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * dingwen
 * 2021/4/4 22:36
 **/
@Api(tags = "成绩计算规则管理")
@RestController
@RequestMapping("/rule")
public class RuleController {
    private final RuleService ruleService;

    @Autowired
    RuleController(RuleService ruleService) {
        this.ruleService = ruleService;
    }

    /*
     *添加规则信息
     * @param [rule]
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation("添加规则信息")
    @ApiImplicitParam(value = "规则实体", name = "rule", required = true, dataType = "rule", paramType = "body")
    @PostMapping("/add")
    public Result add(@RequestBody Rule rule) {
        try {
            new RuleValidaImpl().valida(ValidaEnum.ADD, rule);
        } catch (ServiceException serviceException) {
            return serviceException.result();
        }
        boolean flag = ruleService.save(rule);
        return flag ? ResultGenerator.genOkResult(rule) : ResultGenerator.genFailureResult("添加规则信息失败");
    }

    /*
     *更新规则信息
     * @param [rule]
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation("更新规则信息")
    @ApiImplicitParam(value = "规则实体", name = "rule", required = true, dataType = "rule", paramType = "body")
    @PutMapping("/modify")
    public Result modify(@RequestBody Rule rule) {
        try {
            new RuleValidaImpl().valida(ValidaEnum.MODIFY, rule);
        } catch (ServiceException serviceException) {
            return serviceException.result();
        }
        boolean flag = ruleService.update(rule, new UpdateWrapper<Rule>()
                .and(wrapperCourseId -> wrapperCourseId.eq("course_id", rule.getCourseId()))
                .and(wrapperTeacherId -> wrapperTeacherId.eq("teacher_id", rule.getTeacherId())));
        return flag ? ResultGenerator.genOkResult(rule) : ResultGenerator.genFailureResult("更新规则信息失败");
    }

    /*
     *查询所有规则
     * @param []
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation(value = "查询所有规则")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseId", value = "课程编号", required = false),
            @ApiImplicitParam(name = "courseName", value = "课程名称", required = false),
            @ApiImplicitParam(name = "teacherName", value = "授课教师", required = false),
            @ApiImplicitParam(name = "teacherId", value = "教工号", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页多少条", required = true),
            @ApiImplicitParam(name = "pageNum", value = "第几页", required = true),
    })

    @GetMapping("info/page")
    public Result get(@RequestParam(value = "courseId", required = false) String courseId,
                      @RequestParam(value = "courseName", required = false) String courseName,
                      @RequestParam(value = "teacherId", required = false) String teacherId,
                      @RequestParam(value = "teacherName", required = false) String teacherName,
                      @RequestParam(value = "pageSize") Integer pageSize,
                      @RequestParam("pageNum") Integer pageNum, HttpServletRequest request) {
        Map<String, Object> params = RequestUtil.getParameterMap(request);
        params.put("offset", ((pageNum - 1) * pageSize));
        params.put("limit", (pageSize));
        List<RuleDto> ruleInfoList = ruleService.findRuleInfoList(params);
        return ResultGenerator.genOkResult(ruleInfoList);
    }

    /*
     *查询规则表总记录条数
     * @param []
     * @return com.cyq.dascma.result.Result
     */
    @ApiOperation("查询规则表总记录条数")
    @GetMapping("/total")
    public Result getTotal() {
        return ResultGenerator.genOkResult(ruleService.count());
    }


    /*
     * 根据课程编号 & 授课教师编号 删除规则信息
     * @param courseId
     * @return Result
     */
    @ApiOperation("根据课程编号删除规则信息")
    @ApiImplicitParam(name = "courseId", value = "课程编号", required = true, dataType = "String")
    @DeleteMapping("/remove/{courseId}/{teacherId}")
    public Result remove(@PathVariable("courseId") String courseId, @PathVariable("teacherId") String teacherId) {
        boolean flag = ruleService.remove(new QueryWrapper<Rule>()
                .and(wrapperCourseId -> wrapperCourseId.eq("course_id", courseId))
                .and(wrapperTeacherId -> wrapperTeacherId.eq("teacher_id", teacherId)));
        return flag ? ResultGenerator.genOkResult("ok") : ResultGenerator.genFailureResult("删除失败");
    }

}
